﻿<%@ page language="java" import="com.newcrm.config.Const,com.newcrm.config.Config,com.newcrm.util.Util,com.newcrm.bean.ExtInfo,com.newcrm.bean.User,com.newcrm.bean.Group,com.newcrm.bean.CustomerInfo,com.newcrm.bean.CompanyInfo,java.util.List,java.util.Map" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
  User user = (User) session.getAttribute("user");
  String customerType = request.getParameter("customerType");
  boolean isPeople = "people".equals(customerType);
  CustomerInfo customerInfo = (CustomerInfo)request.getAttribute("customerInfo");

  Map<Integer, CompanyInfo> companys = (Map<Integer, CompanyInfo>) application.getAttribute(Const.APPLICATION_COMPANYS);
  CompanyInfo companyInfo = companys.get(Integer.valueOf(user.companyId));
  List<Map<String, Object>> customerCategorys = companyInfo.customerCategorys;
  List<User> users = companyInfo.users;
  List<Group> allGroups = companyInfo.getAllGroups();

  Config typeConfig = Config.getInstance();
%>
    <tr>
      <th><h2>电话</h2></th>
      <td>
        <div class="contact_forms">
        <div id="blank_slate_<%=Const.CUSTOMER_EXT_INFO_TYPE_PHONE%>" class='blank_slate'>填写联系电话</div>
        <div class="contact_methods" style="display:none;">
          <div id="phone_show">
            <%
            List<ExtInfo> extInfos = customerInfo.extInfos;
            boolean isEmpty = true;
            StringBuffer showContactFormsId = new StringBuffer("");
            for (ExtInfo extInfo : extInfos) {
              if (Const.CUSTOMER_EXT_INFO_TYPE_PHONE == extInfo.type) { %>
            <div class="contact_method">
              <input class="autofocus" name="phone" size="30" type="text" value="<%=Util.HTMLEncode(extInfo.info1)%>" />
              <input name="phoneId" type="hidden" value="<%=extInfo.id%>" />
              <select name="phoneType">
<%for (String type:typeConfig.phoneType) {%>
            <option value="<%=type%>" <%=Util.selectOptionStatus(extInfo.info2, type)%>><%=type%></option>
<%}%>
               </select>
              <a title="删除" href="<%=extInfo.id%>" class="remove"><i class="fa fa-times"></i></a>
            </div>
            <%
                 isEmpty = false;
                }
              }
              if (isEmpty) {
             %>
            <div class="contact_method">
              <input class="autofocus" name="phone" size="30" type="text" value="" />
              <select name="phoneType">
<%for (String type:typeConfig.phoneType) {%>
                <option value="<%=type%>"><%=type%></option>
<%}%>
              </select>
              <a title="删除" href="#" class="remove"><i class="fa fa-times"></i></a>
            </div>
            <%
              } else {
                showContactFormsId = showContactFormsId.append(Const.CUSTOMER_EXT_INFO_TYPE_PHONE).append(",");
              }
            %>
          </div>
          <div class="add_contact_method">
            <div class="add"><a href="phone_show"><i class="fa fa-plus"></i> 再添加一个</a></div>
          </div>
        </div></div></td>
    </tr>
    <tr>
      <th><h2>邮箱</h2></th>
      <td>
        <div class="contact_forms" >
        <div id="blank_slate_<%=Const.CUSTOMER_EXT_INFO_TYPE_EMAIL%>" class='blank_slate'>填写联系邮箱</div>
        <div class="contact_methods" style="display:none;">
          <div id="email_show">
            <%
              isEmpty = true;
              for (ExtInfo extInfo : extInfos) {
                if (Const.CUSTOMER_EXT_INFO_TYPE_EMAIL == extInfo.type) {
            %>
            <div class="contact_method">
              <input class="autofocus" name="email" size="30" type="text" value="<%=Util.HTMLEncode(extInfo.info1)%>" />
              <input name="emailId" type="hidden" value="<%=extInfo.id%>" />
              <select name="emailType">
<%for (String type:typeConfig.emailType) {%>
              <option value="<%=type%>" <%=Util.selectOptionStatus(extInfo.info2, type)%>><%=type%></option>
<%}%>
              </select>
              <a title="删除" href="<%=extInfo.id%>" class="remove"><i class="fa fa-times"></i></a>
            </div>
            <%
                  isEmpty = false;
                }
              }
              if (isEmpty) {%>
            <div class="contact_method">
            <input class="autofocus" name="email" size="30" type="text" />
            <select name="emailType">
<%for (String type:typeConfig.emailType) {%>
              <option value="<%=type%>"><%=type%></option>
<%}%>
            </select>
            <a title="删除" href="#" class="remove"><i class="fa fa-times"></i></a>
            </div>
            <%
              } else {
                showContactFormsId = showContactFormsId.append(Const.CUSTOMER_EXT_INFO_TYPE_EMAIL).append(",");
              }
            %>
          </div>
          <div class="add_contact_method">
            <div class="add"><a href="email_show"><i class="fa fa-plus"></i> 再添加一个</a></div>
          </div>
        </div></div></td>
    </tr>
    <tr>
      <th><h2>聊天工具</h2></th>
      <td>
        <div class="contact_forms">
        <div id="blank_slate_<%=Const.CUSTOMER_EXT_INFO_TYPE_IM%>" class='blank_slate'>填写QQ，旺旺等聊天帐号</div>
        <div class="contact_methods" style="display:none;">
          <div id="im_show">
             <%
              isEmpty = true;
              for (ExtInfo extInfo : extInfos) {
                if (Const.CUSTOMER_EXT_INFO_TYPE_IM == extInfo.type) {
             %>
            <div class="contact_method">
            <input class="autofocus" name="im" size="30" type="text" value="<%=Util.HTMLEncode(extInfo.info1)%>" />
            <input name="imId" type="hidden" value="<%=extInfo.id%>" />
            <select name="imType1">
<%for (String type:typeConfig.imType) {%>
            <option value="<%=type%>" <%=Util.selectOptionStatus(extInfo.info3, type)%>><%=type%></option>
<%}%>
            </select>
            <select name="imType">
            <option value="工作" <%=Util.selectOptionStatus(extInfo.info2, "工作")%>>工作</option>
            <option value="个人" <%=Util.selectOptionStatus(extInfo.info2, "个人")%>>个人</option>
            <option value="其他" <%=Util.selectOptionStatus(extInfo.info2, "其他")%>>其他</option></select>
            <a title="删除" href="<%=extInfo.id%>" class="remove"><i class="fa fa-times"></i></a>
            </div>
            <%
                  isEmpty = false;
                }
              }
              if (isEmpty) {%>
            <div class="contact_method">
            <input class="autofocus" name="im" size="30" type="text" value="" />&nbsp;
            <select name="imType1">
<%for (String type:typeConfig.imType) {%>
            <option value="<%=type%>"><%=type%></option>
<%}%>
            </select>
            <select name="imType">
            <option value="工作">工作</option>
            <option value="个人">个人</option>
            <option value="其他">其他</option></select>
            <a title="删除" href="#" class="remove"><i class="fa fa-times"></i></a>
            </div>
            <%
              } else {
                showContactFormsId = showContactFormsId.append(Const.CUSTOMER_EXT_INFO_TYPE_IM).append(",");
              }
            %>
          </div>
          <div class="add_contact_method">
            <div class="add"><a href="im_show"><i class="fa fa-plus"></i> 再添加一个</a></div>
          </div>
        </div></div></td>
    </tr>
<%if (isPeople) {%>
    <tr>
      <th><h2>微博</h2></th>
      <td>
        <div class="contact_forms">
          <div id="blank_slate_<%=Const.CUSTOMER_EXT_INFO_TYPE_WEIBO%>" class='blank_slate'>填写微博帐号</div>
          <div class="contact_methods" style="display:none;">
            <div id="weibo_show">
              <%
                isEmpty = true;
                for (ExtInfo extInfo : extInfos) {
                  if (Const.CUSTOMER_EXT_INFO_TYPE_WEIBO == extInfo.type) {
              %>
              <div class="contact_method">
              <input class="autofocus" name="weibo" size="30" type="text" value="<%=Util.HTMLEncode(extInfo.info1)%>" />
              <input name="weiboId" type="hidden" value="<%=extInfo.id%>" />
              <select name="weiboType">
<%for (String type:typeConfig.weiboType) {%>
              <option value="<%=type%>" <%=Util.selectOptionStatus(extInfo.info2, type)%>><%=type%></option>
<%}%>
              </select>
              <a title="删除" href="<%=extInfo.id%>" class="remove"><i class="fa fa-times"></i></a>
              </div>
              <%
                    isEmpty = false;
                  }
                }
                if (isEmpty) { %>
              <div class="contact_method">
              <input class="autofocus" name="weibo" size="30" type="text" value="" />
              <select name="weiboType">
<%for (String type:typeConfig.weiboType) {%>
            <option value="<%=type%>"><%=type%></option>
<%}%>
              </select>
              <a title="删除" href="#" class="remove"><i class="fa fa-times"></i></a>
              </div>
            <%
                } else {
                  showContactFormsId = showContactFormsId.append(Const.CUSTOMER_EXT_INFO_TYPE_WEIBO).append(",");
                }
            %>
            </div>
            <div class="add_contact_method">
              <div class="add"><a href="weibo_show"><i class="fa fa-plus"></i> 再添加一个</a></div>
            </div>
          </div></div></td>
    </tr>
    <%}%>
    <tr>
    <th><h2>网站</h2></th>
      <td>
        <div class="contact_forms">
          <div id="blank_slate_<%=Const.CUSTOMER_EXT_INFO_TYPE_WEBSITE%>" class='blank_slate'>填写网址</div>
          <div class="contact_methods" style="display:none;">
            <div id="website_show">
            <%
              isEmpty = true;
              for (ExtInfo extInfo : extInfos) {
                if (Const.CUSTOMER_EXT_INFO_TYPE_WEBSITE == extInfo.type) {
            %>
              <div class="contact_method">
              <input class="autofocus" name="website" size="30" type="text" value="<%=Util.HTMLEncode(extInfo.info1)%>" />
              <input name="websiteId" type="hidden" value="<%=extInfo.id%>" />
              <select name="websiteType">
<%for (String type:typeConfig.websiteType) {%>
              <option value="<%=type%>" <%=Util.selectOptionStatus(extInfo.info2, type)%>><%=type%></option>
<%}%>
             </select>
              <a title="删除" href="<%=extInfo.id%>" class="remove"><i class="fa fa-times"></i></a>
              </div>
            <%
                  isEmpty = false;
                }
              }
              if (isEmpty) {
            %>
              <div class="contact_method">
              <input class="autofocus" name="website" size="30" type="text" value="" />
              <select name="websiteType">
<%for (String type:typeConfig.websiteType) {%>
              <option value="<%=type%>"><%=type%></option>
<%}%>
              </select>
              <a title="删除" href="#" class="remove"><i class="fa fa-times"></i></a>
              </div>
            <%
              } else {
                showContactFormsId = showContactFormsId.append(Const.CUSTOMER_EXT_INFO_TYPE_WEBSITE).append(",");
              }
            %>
            </div>
            <div class="add_contact_method">
              <div class="add"><a href="website_show"><i class="fa fa-plus"></i> 再添加一个</a></div>
            </div>
         </div></div></td>
    </tr>
    <tr>
      <th><h2>地址</h2></th>
      <td>
        <div class="contact_forms addresses">
        <div id="blank_slate_<%=Const.CUSTOMER_EXT_INFO_TYPE_ADDRESS%>" class='blank_slate'>填写联系地址</div>
        <div class="contact_methods" style="display:none;">
          <div id="addresses_show">
             <%
                isEmpty = true;
                for (ExtInfo extInfo : extInfos) {
                  if (Const.CUSTOMER_EXT_INFO_TYPE_ADDRESS == extInfo.type) {
             %>
            <div class="contact_method address">
              <textarea class="autofocus" id="address" name="address" title="地址"><%=Util.HTMLEncode(extInfo.info1)%></textarea>
              <p>
                <select name="province" class="province" onchange="changeProvince($(this), false);">
                <option value="">省份</option><option value="安徽">安徽</option><option value="北京">北京</option><option value="福建">福建</option><option value="甘肃">甘肃</option><option value="广东">广东</option><option value="广西">广西</option><option value="贵州">贵州</option><option value="海南">海南</option><option value="河北">河北</option><option value="河南">河南</option><option value="黑龙江">黑龙江</option><option value="湖北">湖北</option><option value="湖南">湖南</option><option value="吉林">吉林</option><option value="江苏">江苏</option><option value="江西">江西</option><option value="辽宁">辽宁</option><option value="内蒙古自治区">内蒙古</option><option value="宁夏回族自治区">宁夏</option><option value="青海">青海</option><option value="山东">山东</option><option value="山西">山西</option><option value="陕西">陕西</option><option value="上海">上海</option><option value="四川">四川</option><option value="天津">天津</option><option value="西藏自治区">西藏</option><option value="新疆维吾尔自治区">新疆</option><option value="云南">云南</option><option value="浙江">浙江</option><option value="重庆">重庆</option><option value="中国香港">香港</option><option value="中国台湾">台湾</option><option value="中国澳门">澳门</option>
                </select>
                <select name="city"><option value="">城市</option></select>
              </p>
              <div class="loc_remove">
                <select name="addressType">
<%for (String type:typeConfig.addressType) {%>
                <option value="<%=type%>" <%=Util.selectOptionStatus(extInfo.info2, type)%>><%=type%></option>
<%}%>
                </select>
                <a title="删除" href="<%=extInfo.id%>" class="remove"><i class="fa fa-times"></i></a>
              </div>
              <input name="addressId" type="hidden" value="<%=extInfo.id%>" />
              <input name="provinceValue" type="hidden" value="<%=extInfo.info3%>" />
              <input name="cityValue" type="hidden" value="<%=extInfo.info4%>" />
            </div>
            <%
                isEmpty = false;
              }
            }
            if (isEmpty) {
            %>
            <div class="contact_method address">
              <textarea class="autofocus overlayable" id="address" name="address" title="地址"></textarea>
              <p>
                <select name="province" class="province" onchange="changeProvince($(this), false);">
                <option value="">省份</option><option value="安徽">安徽</option><option value="北京">北京</option><option value="福建">福建</option><option value="甘肃">甘肃</option><option value="广东">广东</option><option value="广西">广西</option><option value="贵州">贵州</option><option value="海南">海南</option><option value="河北">河北</option><option value="河南">河南</option><option value="黑龙江">黑龙江</option><option value="湖北">湖北</option><option value="湖南">湖南</option><option value="吉林">吉林</option><option value="江苏">江苏</option><option value="江西">江西</option><option value="辽宁">辽宁</option><option value="内蒙古自治区">内蒙古</option><option value="宁夏回族自治区">宁夏</option><option value="青海">青海</option><option value="山东">山东</option><option value="山西">山西</option><option value="陕西">陕西</option><option value="上海">上海</option><option value="四川">四川</option><option value="天津">天津</option><option value="西藏自治区">西藏</option><option value="新疆维吾尔自治区">新疆</option><option value="云南">云南</option><option value="浙江">浙江</option><option value="重庆">重庆</option><option value="中国香港">香港</option><option value="中国台湾">台湾</option><option value="中国澳门">澳门</option>
                </select>
                <select name="city"><option value="">城市</option></select>
              </p>
              <div class="loc_remove">
                <select name="addressType">
<%for (String type:typeConfig.addressType) {%>
                <option value="<%=type%>"><%=type%></option>
<%}%>
                </select>
                <a title="删除" href="#" class="remove"><i class="fa fa-times"></i></a>
              </div>
            </div>
            <%
            } else {
              showContactFormsId = showContactFormsId.append(Const.CUSTOMER_EXT_INFO_TYPE_ADDRESS).append(",");
            }
            %>
          </div>
          <div id="add_company_address" class="add_contact_method">
            <div class="add"><a href="addresses_show"><i class="fa fa-plus"></i> 再添加一个</a></div>
          </div>
        </div></div></td>
    </tr>
    </table>
  </div>
  <input type="hidden" value="<%=showContactFormsId.toString()%>" id="showContactFormsId">
  <div class="custom_fields">
    <div class="custom_fields_header">
      <h1><a href="#" id="custom_fields_link" title="点击可以展开/缩起自定义信息"><i class="fa fa-plus"></i> 自定义信息</a> </h1>
      <% if (user.isAdmin) {%>
      <a href="<%=request.getContextPath()%>/customerfield" class="link_to"><i class="fa fa-cog"></i> 设置自定义信息</a>
      <% }%>
    </div>
<%
  List<Map<String, Object>> fields = (List<Map<String, Object>>) request.getAttribute("fields");
  if (fields != null) {
%>
    <table id="custom_fields_table" cellpadding="0" cellspacing="0">
<%
    for (Map<String, Object> field:fields) {
      int extInfoId = Util.str2Int(field.get("CustomerFieldId"));
      int fieldType = Util.str2Int(field.get("FieldType"));
      int valueType = Util.str2Int(field.get("ValueType"));
      String initValue = (String) field.get("InitValue");

      ExtInfo extInfo = customerInfo.getExtInfo(extInfoId);
      if (extInfo == null) {
        extInfo = new ExtInfo();
      }
      if ((fieldType == Const.CUSTOMER_FIELD_TYPE_ALL) || (!isPeople && fieldType == Const.CUSTOMER_FIELD_TYPE_COMPANY) || (isPeople && fieldType == Const.CUSTOMER_FIELD_TYPE_PEOPLE)) {
%>
    <tr>
    <th><span><%=Util.HTMLEncode((String)field.get("CustomerFieldName"))%></span></th>
    <td>
      <input name="<%=Const.FIELDID_KEY%>_<%=extInfoId%>" type="hidden" value="<%=extInfo.id%>" />
      <%if (valueType == Const.FIELD_TYPE_DATE) {%>
       <input class="datepickerinput" name="<%=Const.FIELD_KEY%>_<%=field.get("CustomerFieldId")%>" size="30" value="<%=Util.HTMLEncode(extInfo.info1)%>" type="text" />
       <%} else if (valueType == Const.FIELD_TYPE_SELECT) {%>
       <select name="<%=Const.FIELD_KEY%>_<%=field.get("CustomerFieldId")%>">
        <option value="">请选择</option>
       <%
        if (initValue != null && initValue.length() > 0) {
          for (String v:initValue.split(",")) {
            String tmp = "";
            if (v.equals(extInfo.info1)) {
              tmp = "selected";
            }
          %>
        <option value="<%=Util.HTMLEncode(v)%>" <%=tmp%>><%=Util.HTMLEncode(v)%></option>
       <%  }
        }%>
       </select>
       <%} else if (valueType == Const.FIELD_TYPE_CHECKBOX) {
          if (initValue != null && initValue.length() > 0) {
            for (String v:initValue.split(",")) {
              String tmp = "";
              if (extInfo.info1 != null && extInfo.info1.length() > 0) {
                for (String t:extInfo.info1.split(",")) {
                  if (v.equals(t)) {
                    tmp = "checked";
                    break;
                  }
                }
              }
            %>
          <label><input type="checkbox" name="<%=Const.FIELD_KEY%>_<%=field.get("CustomerFieldId")%>" value="<%=Util.HTMLEncode(v)%>" <%=tmp%>> <%=Util.HTMLEncode(v)%></label>
       <%   }
          }
        } else if (valueType == Const.FIELD_TYPE_NUMBER) { %>
      <input class="ime-disabled" name="<%=Const.FIELD_KEY%>_<%=field.get("CustomerFieldId")%>" id="<%=Const.FIELD_KEY%>_<%=field.get("CustomerFieldId")%>_number" value="<%=Util.HTMLEncode(extInfo.info1)%>" size="30" type="text" />
      <script>
        $('#<%=Const.FIELD_KEY%>_<%=field.get("CustomerFieldId")%>_number').bind('keypress', function(e) {
          return ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
        });
      </script>
      <%} else {%>
      <input name="<%=Const.FIELD_KEY%>_<%=field.get("CustomerFieldId")%>" size="30" value="<%=Util.HTMLEncode(extInfo.info1)%>" type="text" />
       <%}%>
    </td>
    </tr>
<%
      }
    }
%>
    </table>
<%
  }
%>
  </div>
  <div class="edit_section background">
    <h5>简介<span> (介绍，如何认识的等)</span></h5>
    <textarea cols="40" id="background" name="background" rows="20"><%=Util.HTMLEncode(customerInfo.note)%></textarea>
  </div>
  <% if (!customerInfo.hasModifyRight(user, users)) {%>
  <input type="hidden" name="hasModifyRight" value="0">
  <% } else {%>
  <input type="hidden" name="hasModifyRight" value="1">
  
  <div class="visibility edit_section">
    <h5>权限设置</h5>
    <div class="form_section">
      <div class="select_permissions">
        <div class="scope">
          <label><input name="visible" value="<%=Const.VISIBLE_EVERYONE%>" type="radio"/> 公海</label>
            <select name="groupPermissionsId" id="groupPermissionsId">
              <jsp:include page="commonPublicDataSelection.jsp" flush="true"/>
            </select>
        </div>
        
        <div class="scope">
          <label><input name="visible" value="<%=Const.VISIBLE_PERSON%>" checked type="radio"/> 私有数据 </label>
          <div class="editor" style="display:none;">
          <select name="permissionsId" id="permissionsId">
            <jsp:include page="commonUserSelection.jsp" flush="true"/>
          </select>
          <div class="shareUsers">
              <p><b>同时，共享给如下的同事</b></p>
                <!--
                <div id="share_group" class="scope">
                <label>部门:</label>
                  <div class="editor">
                  <% if (allGroups != null) {
                      for (Group group : allGroups) {%>
                    <label id="shareGroupId_<%=group.getId()%>"><input name="shareGroupId" type="checkbox" value="<%=group.getId()%>" /> <%=Util.HTMLEncode(group.getName())%></label>
                  <%  }
                     } 
                  %>
                  </div>
                </div>
                -->
                <div id="share_person" class="scope">
                  <label>个人:</label>
                  <div class="editor">
                  <%
                    String groupNameForShare = null;
                    for (User u : users) {
                      if (u.userId.equals(user.userId)) {
                        continue;
                      }
                      String gn = companyInfo.getGroupName(u);
                      if (gn != null && !gn.equals(groupNameForShare)) {
                        groupNameForShare = gn;  
                  %>
                      <p><%=Util.HTMLEncode(groupNameForShare)%></p>
                      <%}%>
                      <label id="shareUserId_<%=u.userId%>"><input name="shareUserId" type="checkbox" value="<%=u.userId%>" /> <%=Util.HTMLEncode(u.name)%></label>
                 <% } %>
                  </div>
                </div>
             </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <%}%>